/*
 * FileUtils.java
 *
 * Created on 15 April 2007, 10:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.cardcopier.file;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileSystemUtils;

/**
 *
 * @author Mark
 */
public class FileUtils {
    
    public FileUtils() {
    }
    
    public void copyFile(File source, File destination) throws IOException {
        org.apache.commons.io.FileUtils.copyFile(source, destination, true);
        long sourceCheckSum = org.apache.commons.io.FileUtils.checksumCRC32(source);
        long destinationCheckSum = org.apache.commons.io.FileUtils.checksumCRC32(destination);
        if (sourceCheckSum != destinationCheckSum) {
            throw new IOException("Destination file checksum did not match source file checksum!");
        }
    }

    public void deleteFile(File file) throws IOException {
        org.apache.commons.io.FileUtils.forceDelete(file);
    }
    
    public void deleteDirAndContents(File directory) throws IOException {
        org.apache.commons.io.FileUtils.deleteDirectory(directory);
    }
    
    public long spaceAvailableInBytes(File path) throws IOException {
        return FileSystemUtils.freeSpaceKb(path.getAbsolutePath()) * org.apache.commons.io.FileUtils.ONE_KB;
    }  
    
    public long spaceUsedInBytes(File directory) {
        return org.apache.commons.io.FileUtils.sizeOfDirectory(directory);
    }
    
    public long spaceUsedInBytes(FileLocation fileLocation) {
        long spaceUsed = 0;
        for (File file : fileLocation.getFiles()) {
            spaceUsed += file.length();
        }
        return spaceUsed;
    }
}
